<?php
/*
 * @Author: 莫卓才
 * @eMail: handsome.mo@foxmail.com
 * @Descripttion: 描述
 * @version: 1.0.0
 * @Date: 2020-03-24 16:03:15
 * @LastEditors: 莫卓才
 * @LastEditTime: 2020-07-04 17:13:22
 */
declare (strict_types = 1);

namespace app\index\controller;

use app\common\model\NoticeModel;
use app\common\model\NoticeCategoryModel;
use think\facade\View;

class Notice extends Common
{
	protected function initialize()
	{
		parent::initialize(); // TODO: Change the autogenerated stub


	}

	public function index()
	{
		$cid = input('cid',1);
		$map = [];
		if(!empty($cid)){
			$categoryInfo = NoticeCategoryModel::where('id',$cid)->find();
			if($categoryInfo->pid===0){
				$categorySubIdArr = NoticeCategoryModel::where('pid',$categoryInfo->id)->column('id');
				array_push($categorySubIdArr,$cid);
				$map[] = ['category_id','in',$categorySubIdArr];
			}else{
				$map[] = ['category_id','=',$cid];
			}

		}


		$map[] = ['lang','=',$this->lang];
		$map[] = ['status','=',1];
		$list = NoticeModel::pageList($map,5);
		// 获取分页显示
		$page = $list->render();


    $currentCategory = NoticeCategoryModel::where('id',$cid)->find();
    if($this->lang=='en'){
      View::assign('currentCategoryName',$currentCategory->ename);
    }else{
      View::assign('currentCategoryName',$currentCategory->name);
    }
		View::assign('cid',$cid);


		if(!empty($currentCategory->pid)){
			$categoryPid = $currentCategory->pid;
		}else{
			$categoryPid = $currentCategory->id;

		}
		$currentCategoryList = NoticeCategoryModel::where('pid',$categoryPid)->whereOr('id',$categoryPid)->order('pid ASC')->select();
		foreach ($currentCategoryList as $key=>$val){
			if($this->lang=='en'){
				$currentCategoryList[$key]['name'] = $val['ename'];
			}
		}
		View::assign('currentCategoryList',$currentCategoryList);

		return view('index',['list'=>$list,'page'=>$page]);
	}

	public function show(){
		$id = input('id');
		$act = input('act','');
		$info = NoticeModel::info($id);

		if($info->status==0&&$act!='view'){
			echo '没有权限';
			exit;
		}

		NoticeModel::where('id', $id)->inc('clicks', 1)->update();


		$prev = NoticeModel::prev($info);
		$next = NoticeModel::next($info);

		View::assign('info',$info);
		View::assign('prev',$prev);
		View::assign('next',$next);

		if(!empty($info)){
			$currentCategory = NoticeCategoryModel::where('id',$info->category_id)->find();
			if($this->lang=='en'){
				$currentCategory->name = $currentCategory->ename;
			}

			View::assign('currentCategoryName',$currentCategory->name);
			View::assign('cid',$info->category_id);

			if(!empty($currentCategory->pid)){
				$categoryPid = $currentCategory->pid;
			}else{
				$categoryPid = $currentCategory->id;

			}
			$currentCategoryList = NoticeCategoryModel::where('pid',$categoryPid)->whereOr('id',$categoryPid)->order('pid ASC')->select();
			View::assign('currentCategoryList',$currentCategoryList);
		}

		return view('info');
	}

	public function search(){
		$keyword = input('keyword');
		$map = [];
		if(!empty($keyword)){
			$map[] = ['title','like','%'.$keyword.'%'];
		}
		$map[] = ['status','=',1];
		$list = NoticeModel::pageList($map,10);
		// 获取分页显示
		$page = $list->render();

		return view('',['list'=>$list,'page'=>$page]);
	}

	public function test(){
		$list = get_notice_list(0,12);
		print_r($list);


	}
}
